<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['union:qrcode:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="生成时间" align="center" prop="createTime" />
      <el-table-column label="下载地址" show-overflow-tooltip align="center" prop="downloadUrl">
        <template slot-scope="scope">
          <span>
            <el-link :href="scope.row.downloadUrl" type="primary">{{ scope.row.downloadUrl }}</el-link>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="生成数量" align="center" prop="createCount" />
      <el-table-column label="生成用户" align="center" prop="createUser" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="180px">
        <el-form-item prop="createCount" label="生成数量">
          <el-input-number :min="1" :max="500" v-model="form.createCount" />
        </el-form-item>

        <el-form-item prop="createType" label="二维码类型">
          <el-select v-model="form.createType" placeholder="请选择二维码类型" clearable>
            <el-option label="A类" value="A"></el-option>
            <el-option label="B类" value="B"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createQr, downloadList } from '@/api/union/qrcode'
export default {
  name: 'qrlist',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      title: "生成二维码",
      open: false,
      showSearch: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {}

    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true
      console.log(this.queryParams)
      downloadList(this.queryParams).then(response => {
        this.list = response.data.rows
        this.loading = false
        this.total = response.data.total
      })
    },
    handleAdd() {
      this.open = true;
      this.reset();
    },
    reset() {
      this.form = {
        createCount: 1,
        createType: "A"
      }
    },
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm() {
      createQr(this.form).then(response => {
        if (response.code === 200) {
          this.msgSuccess("操作成功")
        } else {
          this.msgError("操作失败")
        }

        this.open = false;
        this.getList();
      })
    }
  }
}
</script>
